#include "BatMonitor.h"
#include <stdio.h>
#include <stdlib.h>
#include "bat_afe.h"
#include "temp.h"
#include "drv_timer.h"
#include "debug_uart.h"
#include "ocv_table.h"
#include "dvc.h"

static SysRunData_t  *runinfo = NULL;
static SysConfData_t *confinfo = NULL;	

static void mos_board_update_temp(void){
	uint16_t adcVol = ((uint16_t)get_adc_vol_value(0))%3300;//环境温度
	runinfo->envTemp = get_temp_by_vol(adcVol,3435.0);
	adcVol = ((uint16_t)get_adc_vol_value(1))%3300;//MOS温度
	runinfo->mosTemp = get_temp_by_vol(adcVol,3435.0);
}


void bat_data_update(void){
	static uint8_t times=0;
	if(get_dvc11xx_alert_flag()==1){
		dvc11xx_alert_flag_clear();
		afe_update_current();	//更新电流
		afe_update_status();	//更新故障状态和MOS状态	
		if(afe_update_cellvol()){	//更新电压完成
			firstUpdateOcvSoc();
		}
		afe_balance_policy();
		afe_update_bat_temp();//更新电池温度
		mos_board_update_temp();//更新MOS和板子温度			
	}
}

void bat_monitor_init(void){
	static Timer_t *bat_monitor_tmier_100ms = NULL;
	runinfo = getSysRunData();
	confinfo = getSysConfData();
	afe_init();
	
	Judge_BatNum();
//	COW_enable();
	chgOptionEnable(Enable);
	dsgOptionEnable(Enable);

}